<template>
    <view class="page container">
      <u-form
        :model="basicForm"
        :rules="rules"
        labelPosition="top"
        ref="form"
        labelWidth="auto"
        :labelStyle="{
          fontSize: '14px',
          color: '#B2B2B2',
          lineHight: '20px',
        }"
      >
        <view class="basic-style">
          <u-form-item label="姓名" prop="empId" :borderBottom="false" required>
            <!-- <h-select-person-input
					  style="width: 100%"
					  v-model="basicForm.empId"
					  placeholder="请选择部门负责人"
					  border="surround"
					  isMulti
                      @input="detailBack"
					  suffixIcon="arrow-down-fill"
					  :suffixIconStyle="{ color: '#B2B2B2' }"
					  objectField="basicForm.empId"
					></h-select-person-input> -->
            <h-select-person-input-popup
                            style="width: 100%"
                            v-model="basicForm.empId"
                            :disabled="oneProjectSpecialist"
                            placeholder="请选择人员"
                            border="surround"
                            @back="dataBack"
                            suffixIcon="arrow-down-fill"
                            :suffixIconStyle="{ color: '#B2B2B2' }"
                            objectField="basicForm.empId"
                          ></h-select-person-input-popup>
          </u-form-item>
          <u-form-item label="所属团队" prop="teamId" required>
            <h-select-team-input-popup
                          style="width: 100%;"
                          v-model="basicForm.teamId"
                          :disabled="oneProjectSpecialist"
                          objectField="basicForm.teamId"
                          placeholder="请选择团队"
                          border="surround"
                          suffixIcon="arrow-down-fill"
                          :suffixIconStyle="{color: '#B2B2B2'}"
                          :tdlx="1"
                      ></h-select-team-input-popup>
            <!-- <h-number-input
              placeholder="请输入合同占比"
              v-model="basicForm.contractRate"
              :digit="2"
              :max="99999999"
              :min="0"
              suffixIcon="edit-pen-fill"
            ></h-number-input> -->
          </u-form-item>
          <u-form-item label="预计审核费" required prop="expectedAuditFee" :borderBottom="false">
            <h-money-input @blur="calculateActualFee" :disabled="twoProjectSpecialist" placeholder="请输入预计审核费" v-model="basicForm.expectedAuditFee"></h-money-input>

            <!-- <u--input :disabled="twoProjectSpecialist" @blur="calculateActualFee" v-model="basicForm.expectedAuditFee" placeholder="请输入预计审核费" border="surround"></u--input> -->
          </u-form-item>
          <u-form-item label="扣除比例" prop="finalNo" :borderBottom="false">
              <h-dict-select
              :disabled="twoProjectSpecialist"
              style="width: 100%" 
              @change="calculateActualFee"
              v-model="basicForm.deductionRatio" 
              placeholder="请选择" 
              dictName="deduction_ratio" 
              suffixIcon="arrow-down-fill" 
              :suffixIconStyle="{color: '#B2B2B2'}"
            ></h-dict-select>
          </u-form-item>
          <u-form-item label="实际审核费" prop="actualAuditFee" :borderBottom="false">
            <!-- <u--input v-model="basicForm.actualAuditFee" disabled placeholder="请输入实际审核费" border="surround"></u--input> -->
            <h-money-input v-model="basicForm.actualAuditFee" disabled placeholder="请输入实际审核费" border="surround"></h-money-input>
          </u-form-item>
          <u-form-item v-if="isSignatureFee" label="签字费" prop="signatureFee" required :borderBottom="false">
            <!-- <u--input v-model="basicForm.signatureFee"  :disabled="twoProjectSpecialist" placeholder="请输入签字费" border="surround"></u--input> -->
            <h-money-input v-model="basicForm.signatureFee"  :disabled="twoProjectSpecialist" placeholder="请输入签字费" border="surround"></h-money-input>
          </u-form-item>
          <!-- <u-form-item label="合同分配额(元)" prop="contractAmount" :borderBottom="false" required>
            <h-money-input placeholder="请输入合同分配额" v-model="basicForm.contractAmount" suffixIcon="edit-pen-fill"></h-money-input>
          </u-form-item> -->
          <!-- <u-form-item label="切割合同额(元)" prop="cutContractAmount" :borderBottom="false" required>
            <h-money-input placeholder="请输入切割合同额" v-model="basicForm.cutContractAmount" suffixIcon="edit-pen-fill"></h-money-input>
          </u-form-item>
          <u-form-item label="生产团队" prop="productTeam" required>
            <h-select-team-input-popup
              style="width: 100%"
              v-model="basicForm.productTeam"
              objectField="basicForm.productTeam"
              placeholder="请选择生产团队"
              border="surround"
                          suffixIcon="arrow-down-fill"
                          :suffixIconStyle="{ color: '#B2B2B2' }"
              :tdlx="3"
              @realChange="handleChangeProductTeam"
            />
          </u-form-item> -->
          <!-- <u-form-item
            label="生产公司"
            prop="productCompanyId"
            :borderBottom="false"
            required>
            <h-select-company-input
              disabled
              style="width: 100%"
              v-model="basicForm.productCompanyId"
              border="surround"
              placeholder="请选择生产公司"
              suffixIcon="arrow-down-fill"
              :suffixIconStyle="{color: '#B2B2B2'}"
              objectField = 'managerTrusteeForm.gsdaId'
            ></h-select-company-input>
             </u-form-item> -->
                  <u-form-item v-if="!remark" label="备注" prop="taskDescription">
                    <u--textarea
                      maxlength="500"
                      :confirm-type="null"
                          count
                          :disabled="twoProjectSpecialist"
                      v-model="basicForm.remark"
                      placeholder="请输入备注"
                    ></u--textarea>
                  </u-form-item>
        </view>
  
        <qian-tree
          ref="qiantree"
          lazy
          :multiple="false"
          :range="getFilesBusinessCategoryList"
          :foldAll="true"
          selectParent
          idKey="categoryCode"
          rangeKey="categoryName"
          @confirm="treeConfirm"
          @cancel="treeCancel"
        ></qian-tree>
  
        <view class="space-x-1 mt-1" style="display: flex">
          <u-button type="primary" text="关闭" :plain="true" :loading="loading" @click="onClose"></u-button>
                  <u-button type="primary" text="保存" :loading="loading" @click="onSubmit"></u-button>
        </view>
      </u-form>
    </view>
  </template>
  
  <script>
  import http from '@/utils/request/index'
  import selectTaskProgressInput from '@/pages/synergy/components/selectTaskProgressInput.vue'
  import {
    filesBusinessCategoryList, // 业务分类列表
  } from '@/api/index.js'
  
  export default {
    name: 'FirstInstanceEngineerModal',
    components: {
      selectTaskProgressInput,
    },
    props: {
      teamId: {
        type: [Number, String],
        default: '',
      },
      companyId: {
        type: [Number, String],
        default: '',
      },
      // 表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false,
      },
      isSignatureFee:{
        type: Boolean,
        default: false,
        required: false,
      },
      datailDisabled: {
        type: Boolean,
        default: false,
      },
      oneProjectSpecialist: {
        type: Boolean,
        default: false,
        required: false
      },
      index: {
        type: Number,
        default: null,
      },
      estimatedContractAmount: {
        type: [Number, String],
        default: 0,
      },
      separateContractAmount: {
        type: [Number, String],
        default: 0,
      },
      remark:{
        type: Boolean,
        default: false,
        required: false,
      },
      twoProjectSpecialist:{
        type: Boolean,
        default: false,
        required: false,
      }
    },
    data() {
      return {
        getFilesBusinessCategoryList: [], // 业务分类列表
        loading: false,
        basicForm: {
          deptId:'',
          empId:'',
          expectedAuditFee:'0',
          actualAuditFee:'0',
          signatureFee:'0',
          remark:'',
          teamId:'',
          deductionRatio:'0',
          attachment:'',
          empNo:'',
          remark:'',
          userType:'two_joint_review'
        //   categoryCode: '',
        //   contractAmount: '',
        //   cutContractAmount: '',
        //   contractRate: '',
        //   productTeam: '',
        //           taskDescription: '',
        //   productCompanyId:'',
        //   productTeam_dictText: '',
        },
        rules: {
          empId: [{ required: true, message: '请选择人员' }],
          teamId: [{ required: true, message: '请选择团队' }],
          expectedAuditFee: [{ required: true, message: '请填写预计审核费' }],
          signatureFee: [{ required: true, message: '请填写签字费' }],
          // contractAmount: [{ required: true, message: '请输入合同分配额' }],
          // cutContractAmount: [{ required: true, message: '请输入切割合同额' }],
          // productTeam: [{ required: true, message: '请选择生产团队' }],
        },
      }
    },
    created() {
      this.getList()
    },
    computed: {
      // 成员匹配合同额 和 任务分配合同额的计算，当改变后触发 watch 重新计算合同额占比
      rate() {
        return [this.basicForm.contractAmount, this.estimatedContractAmount]
      },
    },
    methods: {
        dataBack(data){
            console.log(data,'-----------');
        this.basicForm.deptId = data.departId
        this.basicForm.empNo = data.workNo
        },
        detailBack(data){
            console.log(data,'hhhh-0000');
            
        },
      treeCancel() {
        this.showPopupInput = true
      },
      treeConfirm(item, index) {
        this.$set(this.basicForm, 'categoryCode', item[0].id)
        var serviceName = ''
        if (item[0] && item[0].parents && item[0].parents.length) {
          serviceName = item[0].parents[0].name + '/' + item[0].name
        } else {
          serviceName = item[0].name
        }
  
        this.$set(this.basicForm, 'categoryName', serviceName)
        this.showPopupInput = true
      },
      getList() {
        filesBusinessCategoryList().then((res) => {
          if (res.success) {
            this.getFilesBusinessCategoryList = res.result
          }
        })
      },
      // 点击选择
      onChoice() {
        this.showPopupInput = false
        this.$refs.qiantree._show()
      },
      handleChangeProductTeam(item) {
        if (!item) {
          this.$set(this.basicForm, 'teamLeader', undefined)
          this.$set(this.basicForm, 'teamLeader_dictText', undefined)
          this.$set(this.basicForm, 'productTeam_dictText', undefined)
          this.$set(this.basicForm, 'productCompanyId', undefined)
        } else {
          this.$set(this.basicForm, 'teamLeader', item.tdfzr)
          this.$set(this.basicForm, 'teamLeader_dictText', item.tdfzr)
          this.$set(this.basicForm, 'productTeam_dictText', item.aliasName)
          this.$set(this.basicForm, 'productCompanyId', item.belongCompanyId)
        }
      },
      reset() {
        this.$refs.form.resetFields()
      },
      async onSubmit() {
        await this.$refs.form.validate()
        if (!this.index) {
          this.$emit('submit', [
            {
              id: parseInt(Math.random() * 1000000000000) + '',
              ...this.basicForm,
            },
            this.index,
          ])
        } else {
          this.$emit('submit', [this.basicForm, this.index])
        }
              this.reset()
      },
      onClose() {
        this.$emit('close')
      },
                // 计算实际审核费
        calculateActualFee() {
        // 确保数值类型
        const amount = Number(this.basicForm.expectedAuditFee) || 0
        const rate = Number(this.basicForm.deductionRatio) || 0
        console.log(rate,'rate-----');
        
        // 计算实际费用（预计审核费 * (1 - 扣除比例)）
        const actual = amount * (1 - rate)
        
        // 使用$set确保响应式更新
        this.$set(this.basicForm, 'actualAuditFee', parseFloat(actual.toFixed(2)))
      },
    }
  }
  </script>
  
  <style scoped></style>
  